package Twitter
{
	import mx.collections.ArrayCollection;
	
	public class FilterActions
	{
		import mx.core.Application;
		
		import mx.collections.SortField;
		import mx.collections.Sort;
		
		import mx.utils.ObjectUtil;
		
		//import mx.controls.Alert;
		
		//Lock
		public var LOCK_get_links:Boolean = false; 
		public var LOCK_get_keywords:Boolean = false;
		public var LOCK_refresh_matched_keywords:Boolean = false;
		public var LOCK_get_group_status:Boolean = false;
		
		//public var LOCK_refresh
		
		public function FilterActions()
		{
		}
		
		public function get_links():void
		{
			if (LOCK_get_links==false)
			{
			
			LOCK_get_links = true
						
			Application.application.myLinks = new ArrayCollection();
			
			
			for each (var o:tStatus in Application.application.myStatuses)
			{
				if (o._contains_url)
				{
					Application.application.myLinks.addItem(o);
				}
			}
			var sort:Sort = new Sort();
			sort = new Sort();
			sort.fields = [new SortField("id", false, true)];
			Application.application.myLinks.sort = sort;

			
			/*Application.application.lt_links.dataProvider = Application.application.myLinks;
			
			Application.application.lt_links.dataProvider.refresh();*/
			
			}
			LOCK_get_links = false;
			
			
		}
		
		private function searchLinks(item:tStatus):Boolean{ 
		   var isMatch:Boolean = false
		   if (item._contains_url == true){
		       isMatch = true
		   }               
		   return isMatch;               
		   }
		   
		
		public function refresh_matched_keywords():void
		{
			if (LOCK_refresh_matched_keywords==false)
			{
				LOCK_refresh_matched_keywords = true;
				var tf:tMessageFormat = new tMessageFormat;
				
				try
				{
				
					Application.application.db_prefs.db_keywords();
				}
				catch(e:Error)
				{
					
				}
				
				for each (var o:tStatus in Application.application.myStatuses)
				{
					
					
					o._key_match = tf.match_keywords(o.text);
	
				}
			}
			LOCK_refresh_matched_keywords=false;
			
		}
		public function get_keywords():void
		{
			//Alert.show("keywords= "+arr_keywords.length);
			
			if (LOCK_get_keywords==false)
			{
				LOCK_get_keywords = true;
			refresh_matched_keywords();
			
			Application.application.myKeywords = new ArrayCollection();
			
			for each (var o:tStatus in Application.application.myStatuses)
			{
				trace("Searching keywords");
				trace(Application.application.keywords_with_url);
				if (Application.application.keywords_with_url)
				{
					if (o._key_match && o._contains_url ==true)
					{
						Application.application.myKeywords.addItem(o);
						//var g_key:tStatus = tStatus(ObjectUtil.copy(o));
						//myKeywords.addItem(g_key);
					}
					
				}
				else
				{
					if (o._key_match)
					{
						Application.application.myKeywords.addItem(o);
						//var g_key:tStatus = tStatus(ObjectUtil.copy(o));
						//myKeywords.addItem(g_key);
					}
				}
					
					
				
				
			}
			

			Application.application.counter.get_totals();
			}
			
			LOCK_get_keywords = false;
			
		}
		
		
		
		public function get_group_status():void
		{
			
			if (LOCK_get_group_status==false)
			{
				LOCK_get_group_status = true;
			
			Application.application.myGroupStatuses = new ArrayCollection();
			
			/*for each (var h:GroupMembers in global_group_members)
				{
					Alert.show(h.user_id);
				}*/
				
			//var o_user_id:String = "";
			//var g_user_id:String = "";
			
			
			for each (var o:tStatus in Application.application.myStatuses)
			{
				for each (var g:GroupMembers in Application.application.global_group_members)
				{
					if (o.screen_name == g.user_id)
					{
						//Alert.show("MATCH ID ");
						//o._group_id = g.group_id;
						var g_stat:tStatus = tStatus(ObjectUtil.copy(o));
						g_stat._group_id = g.group_id;
						
						//Alert.show("MATCH ID "+g.group_id)
						Application.application.myGroupStatuses.addItem(g_stat);
					}
				}
			}
			
			//Alert.show("get_group_status() " + myGroupStatuses.length.toString() + "global_group_members = " +global_group_members.length );
			
			var sort:Sort = new Sort();
			sort.fields = [new SortField("id", false, true)];
			Application.application.myGroupStatuses.sort = sort;
			
			/*lt_match_groups.dataProvider = null;
			lt_match_groups.dataProvider = myGroupStatuses;
			
			lt_match_groups.dataProvider.refresh();*/
			
			
			
			if (Application.application.selected_group_status!="ALL GROUPS")
			{
				
				
				Application.application.myGroupStatuses.filterFunction = filterGroupStatus;
				
				Application.application.myGroupStatuses.refresh();
			
			}
			
			}
			LOCK_get_group_status = false;
			
			
		}
		
		private function filterGroupStatus(item:tStatus):Boolean{
		   var isMatch:Boolean = false
		   if (item._group_id == Application.application.selected_group_status){
		       isMatch = true
		   }               
		   return isMatch;               
		   }
		
		
		
		
		

		}
		
		
	
	
}